package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.order.client.model.entity.OrderStats1;
import com.tebiecloud.order.server.service.OrderStats1Service;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 订单统计，维度：门店，手机 前端控制器
 * @author tebie
 * @date 2019-10-07
 */
@RestController
@RequestMapping("/orderstats1")
public class OrderStats1Controller {

    @Autowired
    private OrderStats1Service orderStats1Service;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<OrderStats1>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        OrderStats1 query = pageParams.mapToObject(OrderStats1.class);
        QueryWrapper<OrderStats1> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(orderStats1Service.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<OrderStats1> get(@RequestParam("id") Long id) {
        OrderStats1 entity = orderStats1Service.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param unionid 第三方id,
     * @param userId 用户id,
     * @param storeId 门店id,
     * @param nickname 昵称,
     * @param remarkName 备注姓名,
     * @param headimgurl 头像,
     * @param phone 手机号码,
     * @param amount 消费总额，单位分,
     * @param waitAmount 待核销金额，单位分
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "unionid", required = false) String unionid,
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "storeId", required = false) Long storeId,
        @RequestParam(value = "nickname", required = false) String nickname,
        @RequestParam(value = "remarkName", required = false) String remarkName,
        @RequestParam(value = "headimgurl", required = false) String headimgurl,
        @RequestParam(value = "phone", required = false) String phone,
        @RequestParam(value = "amount", required = false) Integer amount,
        @RequestParam(value = "waitAmount", required = false) Integer waitAmount
    ) {
        OrderStats1 entity = new OrderStats1();
        entity.setUnionid(unionid);
        entity.setUserId(userId);
        entity.setStoreId(storeId);
        entity.setNickname(nickname);
        entity.setRemarkName(remarkName);
        entity.setHeadimgurl(headimgurl);
        entity.setPhone(phone);
        entity.setAmount(amount);
        entity.setWaitAmount(waitAmount);
        orderStats1Service.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param unionid 第三方id
     * @param userId 用户id
     * @param storeId 门店id
     * @param nickname 昵称
     * @param remarkName 备注姓名
     * @param headimgurl 头像
     * @param phone 手机号码
     * @param amount 消费总额，单位分
     * @param waitAmount 待核销金额，单位分
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "unionid", required = false) String unionid,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "remarkName", required = false) String remarkName,
            @RequestParam(value = "headimgurl", required = false) String headimgurl,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "amount", required = false) Integer amount,
            @RequestParam(value = "waitAmount", required = false) Integer waitAmount
    ) {
        OrderStats1 entity = new OrderStats1();
        entity.setUnionid(unionid);
        entity.setUserId(userId);
        entity.setStoreId(storeId);
        entity.setNickname(nickname);
        entity.setRemarkName(remarkName);
        entity.setHeadimgurl(headimgurl);
        entity.setPhone(phone);
        entity.setAmount(amount);
        entity.setWaitAmount(waitAmount);
        orderStats1Service.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            orderStats1Service.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            orderStats1Service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
